package com.yanqu.road.server.cmd.crossunion;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eCrossUnionRankActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eUnionRankActivityType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.activity.StrongholdActivityProto;
import com.yanqu.road.pb.cookboyactivity.CookBoyActivityProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.pb.orewaractivity.OreWarActivityProto;
import com.yanqu.road.pb.rank.CrossUnionActivityRankProto;
import com.yanqu.road.pb.tradewaractivity.TradeWarActivityProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.AllActivityMgr;
import com.yanqu.road.server.manager.union.CrossUnionActivityDataMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = CrossProtocol.C_CROSS_UNION_ACTIVITY_MEMBER_VALUE,desc = "")
public class CrossUnionMemberRankValueCmd extends CrossCommand {
    private long userId;
    private int activityId;
    private String unionId;
    private int conditionType;
    private int rankType;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossUnionActivityRankDataProto.CrossUnionMemberValueReqMsg reqMsg = CrossUnionActivityRankDataProto.CrossUnionMemberValueReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        unionId = reqMsg.getUnionUid();
        conditionType = reqMsg.getType();
        userId = packet.getPlayerId();
        rankType = reqMsg.getRankType();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        UnionActivityConditionData unionActivityConditionData = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, conditionType, unionId);
        if (rankType == 1) {
            ActivityInfo activityInfo = AllActivityMgr.getActivityInfo(activityId);
            if ((activityInfo.getType() == eActivityType.UnionRankActivity.getValue() &&
                    activityInfo.getChildType() == eUnionRankActivityType.unionExpRiseRank.getValue())) {
                ActivityProto.ActivityConditionUnionMemberDataRespMsg.Builder respMsg = ActivityProto.ActivityConditionUnionMemberDataRespMsg.newBuilder();
                respMsg.setRet(ret);
                ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder dataMsg = parseActivityConditionUnionMemberDataListMsg(
                        CrossUnionActivityDataMgr.getUserActivityConditionDataList(activityId, conditionType, unionActivityConditionData.getMemberUserIdList()));
                respMsg.setDataList(dataMsg);
                if (activityInfo.getType() == eActivityType.UnionRankActivity.getValue()
                        && activityInfo.getChildType() == eUnionRankActivityType.unionExpRiseRank.getValue()) {
                    int unionMemberExpAdd = 0;
                    ActivityProto.ActivityConditionUnionMemberDataListMsg dataList = respMsg.getDataList();
                    for (ActivityProto.ActivityConditionUnionMemberDataTempMsg dataTempMsg : dataList.getDataListList()) {
                        unionMemberExpAdd += Integer.parseInt(dataTempMsg.getValue());
                    }
                    int tradeExpAdd = calcUnionTradeExpAdd(unionActivityConditionData.getValue().intValue(), unionMemberExpAdd);
                    respMsg.setParam(tradeExpAdd);
                }
                YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_ACTIVITY_MEMBER_CONDITION_DATA, respMsg);
                MessageHelper.sendPacket(serverId, userId, message);
            }
            CrossUnionActivityRankProto.CrossUnionActivityConditionUnionMemberDataRespMsg.Builder respMsg = CrossUnionActivityRankProto.CrossUnionActivityConditionUnionMemberDataRespMsg.newBuilder();
            if (null != unionActivityConditionData) {
                ActivityProto.UnionRankMemberConditionDataListMsg.Builder dataMsg =parseUnionRankMemberConditionDataListMsg(
                        CrossUnionActivityDataMgr.getUserActivityConditionDataList(activityId,conditionType,unionActivityConditionData.getMemberUserIdList()));
                respMsg.setDataList(dataMsg);
                if ((activityInfo.getType() == eActivityType.CrossUnionActivity.getValue()
                        && activityInfo.getChildType() == eCrossUnionRankActivityType.unionExpRiseRank.getIntValue()) ) {
                    ActivityProto.UnionRankMemberConditionDataListMsg dataList = respMsg.getDataList();
                    int unionMemberExpAdd = 0;
                    for (ActivityProto.UnionRankMemberConditionDataTempMsg dataTempMsg : dataList.getDataListList()) {
                        unionMemberExpAdd += Integer.parseInt(dataTempMsg.getValue());
                    }
                    int tradeExpAdd = calcUnionTradeExpAdd(unionActivityConditionData.getValue().intValue(), unionMemberExpAdd);
                    respMsg.setParam(tradeExpAdd);
                }
            }else {
                ret = GameErrorCode.E_ACTIVITY_NO_JOIN;
            }
            respMsg.setRet(ret);
            if (CrossUnionActivityDataMgr.isCrossTradeActivity(activityId)) {
                TradeWarActivityProto.TradeWarActivityConditionUnionMemberDataRespMsg.Builder builder = TradeWarActivityProto.TradeWarActivityConditionUnionMemberDataRespMsg.newBuilder();
                builder.setRet(ret);
                ActivityProto.UnionRankMemberConditionDataListMsg.Builder dataMsg = parseUnionRankMemberConditionDataListMsg(
                        CrossUnionActivityDataMgr.getUserActivityConditionDataList(activityId,conditionType,unionActivityConditionData.getMemberUserIdList()));
                builder.setDataList(dataMsg);
                YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_TRADE_WAR_ACTIVITY_MEMBER_CONDITION_DATA, builder);
                MessageHelper.sendPacket(serverId, userId, message);
            }else {

                YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_CROSS_ACTIVITY_UNION_MEMBER_CONDITION_DATA, respMsg);
                MessageHelper.sendPacket(serverId, userId, message);
            }


        } else if (rankType == 2) {
            StrongholdActivityProto.StrongholdActivityConditionUnionMemberDataRespMsg.Builder msg = StrongholdActivityProto.StrongholdActivityConditionUnionMemberDataRespMsg.newBuilder();
            List<UserActivityConditionData> list = CrossUnionActivityDataMgr.getUserActivityConditionDataList(activityId, conditionType, unionActivityConditionData.getMemberUserIdList());
            msg.setRet(0);
            if (list.size() > 0) {
                ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder dataMsg = ActivityProto.ActivityConditionUnionMemberDataListMsg.newBuilder();
                for (UserActivityConditionData data : list) {
                    ActivityProto.ActivityConditionUnionMemberDataTempMsg.Builder builder = ActivityProto.ActivityConditionUnionMemberDataTempMsg.newBuilder();
                    builder.setUserId(data.getUserId());
                    builder.setModifyTime(data.getModifyTime());
                    builder.setNickName(CrossUserMgr.getUserBaseInfo(data.getUserId()).getNickName());
                    builder.setValue(data.getValue().toString());
                    dataMsg.addDataList(builder);
                }
                msg.setDataList(dataMsg);
            }
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_GENERAL_ACTIVITY_MEMBER_CONDITION_DATA, msg);
            MessageHelper.sendPacket(serverId,userId, pbMsg);
        } else if (rankType == 3) {
            OreWarActivityProto.OreWarActivityConditionUnionMemberDataRespMsg.Builder msg = OreWarActivityProto.OreWarActivityConditionUnionMemberDataRespMsg.newBuilder();
            List<UserActivityConditionData> list = CrossUnionActivityDataMgr.getUserActivityConditionDataList(activityId, conditionType, unionActivityConditionData.getMemberUserIdList());
            msg.setRet(0);
            if (list.size() > 0) {
                ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder dataMsg = ActivityProto.ActivityConditionUnionMemberDataListMsg.newBuilder();
                for (UserActivityConditionData data : list) {
                    ActivityProto.ActivityConditionUnionMemberDataTempMsg.Builder builder = ActivityProto.ActivityConditionUnionMemberDataTempMsg.newBuilder();
                    builder.setUserId(data.getUserId());
                    builder.setModifyTime(data.getModifyTime());
                    builder.setNickName(CrossUserMgr.getUserBaseInfo(data.getUserId()).getNickName());
                    builder.setValue(data.getValue().toString());
                    dataMsg.addDataList(builder);
                }
                msg.setDataList(dataMsg);
            }
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_ORE_WAR_ACTIVITY_MEMBER_CONDITION_DATA, msg);
            MessageHelper.sendPacket(serverId,userId, pbMsg);
        } else if (rankType == 4) {
//            if (conditionType == eGamePlayerEventType.StrongholdEliminateUnionRank.getValue()) {
//                StrongholdActivityProto.StrongholdWarActivityConditionUnionMemberDataReqMsg.Builder reqMsg = StrongholdActivityProto.StrongholdWarActivityConditionUnionMemberDataReqMsg.newBuilder();
//                reqMsg.setUnionUid(unionId);
//                reqMsg.setActivityId(activityId);
//                reqMsg.setType(eGamePlayerEventType.StrongholdEliminateUnionRank.getValue());
//                reqMsg.setServerId(serverId);
//                YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_STRONGHOLD_MEMBER_CONDITION_DATA, reqMsg);
//                MessageHelper.sendChannelPacket(serverId, userId, pbMsg);
//                return;
//            }
//            StrongholdActivityProto.StrongholdActivityConditionUnionMemberDataRespMsg.Builder respMsg = StrongholdActivityProto.StrongholdActivityConditionUnionMemberDataRespMsg.newBuilder();
//            respMsg.setRet(ret);
//            if (null != unionActivityConditionData) {
//                ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder dataMsg = parseActivityConditionUnionMemberDataListMsg(
//                        CrossUnionActivityDataMgr.getUserActivityConditionDataList(activityId, conditionType, unionActivityConditionData.getMemberUserIdList()));
//                respMsg.setDataList(dataMsg);
//            }else {
//                ret = GameErrorCode.E_ACTIVITY_NO_JOIN;
//            }
//            respMsg.setRet(ret);
//            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_STRONGHOLD_ACTIVITY_MEMBER_CONDITION_DATA, respMsg);
//            MessageHelper.sendChannelPacket(serverId, userId, message);
        } else if (rankType == 5) {
            CookBoyActivityProto.CookBoyActivityConditionUnionMemberDataRespMsg.Builder respMsg = CookBoyActivityProto.CookBoyActivityConditionUnionMemberDataRespMsg.newBuilder();
            respMsg.setRet(ret);
            if(0 == ret){
                if(null != unionActivityConditionData) {
                    ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder dataMsg = parseActivityConditionUnionMemberDataListMsg(
                            CrossUnionActivityDataMgr.getUserActivityConditionDataList(activityId, conditionType, unionActivityConditionData.getMemberUserIdList()));
                    respMsg.setDataList(dataMsg);
                }
            }
            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_COOK_BOY_ACTIVITY_MEMBER_CONDITION_DATA, respMsg);
            MessageHelper.sendPacket(serverId, userId, message);
        }

    }


    public int calcUnionTradeExpAdd(int unionExpAdd, int unionMemberExpAdd) {
        int unionTradeExpAdd;

        unionTradeExpAdd = unionExpAdd - unionMemberExpAdd;
        if (unionTradeExpAdd < 0) {
            unionTradeExpAdd = 0;
        }
        return unionTradeExpAdd;
    }



    @Override
    public void sendData() {

    }

    public static ActivityProto.UnionRankMemberConditionDataListMsg.Builder parseUnionRankMemberConditionDataListMsg(List<UserActivityConditionData> userActivityConditionDataList) {
        ActivityProto.UnionRankMemberConditionDataListMsg.Builder listMsg = ActivityProto.UnionRankMemberConditionDataListMsg.newBuilder();
        if(null != userActivityConditionDataList){
            for(UserActivityConditionData userActivityConditionData : userActivityConditionDataList) {
                ActivityProto.UnionRankMemberConditionDataTempMsg.Builder msg = parseUnionRankMemberConditionDataTempMsg(userActivityConditionData);
                if(null != msg){
                    listMsg.addDataList(msg);
                }
            }
        }
        return listMsg;
    }

    public static ActivityProto.UnionRankMemberConditionDataTempMsg.Builder parseUnionRankMemberConditionDataTempMsg(UserActivityConditionData userActivityConditionData) {
        if(null == userActivityConditionData){
            return null;
        }
        ActivityProto.UnionRankMemberConditionDataTempMsg.Builder msg = ActivityProto.UnionRankMemberConditionDataTempMsg.newBuilder();
        msg.setUserId(userActivityConditionData.getUserId());
        msg.setValue(userActivityConditionData.getValue().toString());
        msg.setModifyTime(userActivityConditionData.getModifyTime());
        UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(userActivityConditionData.getUserId());
        msg.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        return msg;
    }

    public static ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder parseActivityConditionUnionMemberDataListMsg(List<UserActivityConditionData> userActivityConditionDataList) {
        ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder listMsg = ActivityProto.ActivityConditionUnionMemberDataListMsg.newBuilder();
        if(null != userActivityConditionDataList){
            for(UserActivityConditionData userActivityConditionData : userActivityConditionDataList) {
                ActivityProto.ActivityConditionUnionMemberDataTempMsg.Builder msg = parseActivityConditionUnionMemberDataTempMsg(userActivityConditionData);
                if(null != msg){
                    listMsg.addDataList(msg);
                }
            }
        }
        return listMsg;
    }

    public static ActivityProto.ActivityConditionUnionMemberDataTempMsg.Builder parseActivityConditionUnionMemberDataTempMsg(UserActivityConditionData userActivityConditionData) {
        if(null == userActivityConditionData){
            return null;
        }
        ActivityProto.ActivityConditionUnionMemberDataTempMsg.Builder msg = ActivityProto.ActivityConditionUnionMemberDataTempMsg.newBuilder();
        msg.setUserId(userActivityConditionData.getUserId());
        msg.setNickName(CrossUserMgr.getUserName(userActivityConditionData.getUserId()));
        msg.setValue(userActivityConditionData.getValue().toString());
        msg.setModifyTime(userActivityConditionData.getModifyTime());
        return msg;
    }


}
